new Vue({
    el: "#app",
    data: {
        fullscreenLoading: false,
        screenLoading: false,
        dialogDeleteVisible: false,
        dialogNewVisible: false,
        dialogUpdateVisible: false,
        cateData: [],
        id: null,
        newCate: {
            name: '',
            iconPath: ''
        },
        updateCate: {
            id: null,
            name: '',
            iconPath: ''
        },
        onProcess: false,
        uploadPercent: 0,
        uploadIconUrl: '',
        iconImgUrl: '',
        onProcess: false
    },
    mounted: function () {
        var _this = this;
        _this.loadCateList();
    },
    methods: {
        trim: function (str) {
            if (isNaN(str)) {
                return str.replace(/(^\s*)|(\s*$)/g, "");
            } else {
                return str
            }
        },
        loadCateList: function () {
            var _this = this;
            axios.get(request_prefix + "/cash/cate/list")
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.cateData = res.data.data;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        toNewDialog: function () {
            var _this = this;
            _this.uploadIconUrl = '';
            _this.iconImgUrl = '';
            _this.onProcess = false;
            _this.dialogNewVisible = true;
        },
        confirmCreate: function () {
            var _this = this;
            _this.newCate.iconPath = _this.uploadIconUrl;
            if (_this.newCate.name == null || _this.newCate.name == '') {
                _this.$message.warning("类别名称不能为空");
                return;
            }
            if (_this.newCate.iconPath == null || _this.newCate.iconPath == '') {
                _this.$message.warning("类别名称不能为空");
                return;
            }
            axios.post(request_prefix + "/cash/cate/add", _this.newCate)
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.uploadIconUrl = '';
                        _this.iconImgUrl = '';
                        _this.onProcess = false;
                        _this.loadCateList();
                        _this.dialogNewVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.uploadIconUrl = '';
                        _this.iconImgUrl = '';
                        _this.onProcess = false;
                        _this.dialogNewVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        toEditDialog: function (id) {
            var _this = this;
            axios.get(request_prefix + "/cash/cate/detail", {
                params: {
                    id: id
                }
            })
                .then(function (res) {
                    if (res.data.code == 0) {
                        _this.updateCate = res.data.data;
                        _this.uploadIconUrl = _this.updateCate.iconPath;
                        _this.iconImgUrl = _this.updateCate.iconPath;
                        _this.dialogUpdateVisible = true;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        confirmUpdate: function () {
            var _this = this;
            _this.updateCate.iconPath = _this.uploadIconUrl;
            if (_this.updateCate.name == null || _this.updateCate.name == '') {
                _this.$message.warning("类别名称不能为空");
                return;
            }
            if (_this.updateCate.iconPath == null || _this.updateCate.iconPath == '') {
                _this.$message.warning("类别名称不能为空");
                return;
            }
            axios.post(request_prefix + "/cash/cate/update",_this.updateCate)
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.uploadIconUrl = '';
                        _this.iconImgUrl = '';
                        _this.onProcess = false;
                        _this.loadCateList();
                        _this.dialogUpdateVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.uploadIconUrl = '';
                        _this.iconImgUrl = '';
                        _this.onProcess = false;
                        _this.dialogUpdateVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$messgage.error("请求过程中发生错误");
                })
        },
        deleteCate: function (id) {
            var _this = this;
            _this.id = id;
            _this.dialogDeleteVisible = true;
        },
        confirmDelete: function () {
            var _this = this;
            var params = new URLSearchParams();
            params.append("id",_this.id);
            axios.post(request_prefix + "/cash/cate/delete",params)
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.loadCateList();
                        _this.dialogDeleteVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.dialogDeleteVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        handleAvatarProcess: function (event, file, fileList) {
            var _this = this;
            _this.showStatus();
            _this.onProcess = true;
        },
        showStatus: function () {
            var _this = this;
            var intervalId = setInterval(function () {
                if (_this.uploadPercent < 100) {
                    _this.uploadPercent += 5;
                }
            }, 100);
        },
        handleAvatarSuccess: function (res, file) {
            var _this = this;
            _this.uploadIconUrl = res.data.pictureUrl;
            _this.iconImgUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload: function (file) {
            var isJPG = file.type === 'image/jpeg';
            var isPNG = file.type === 'image/png';
            var isGIF = file.type === 'image/gif';
            var isLt1M = file.size / 1024 / 1024 < 1;

            if (!isJPG && !isPNG && !isGIF) {
                this.$message.error('上传图片格式错误!!');
            }

            if (!isLt1M) {
                this.$message.error('上传图片大小不能超过 1MB!');
            }

            if (this.iconImgUrl != '') {
                this.iconImgUrl = '';
            }
            this.uploadPercent = 0;
            this.onProcess = false;

            return (isJPG || isPNG || isGIF) && isLt1M;
        },
        toBackProductList:function () {
            window.location.href = request_prefix + "/cash/product/listPage";
        }
    }
});